﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WP09S03_OfficeTracking
{
    public partial class FrmUser : WP09S03_OfficeTracking.ParentClass.ParentForm
    {
        private Controller.EmployeController controller;
        
        public FrmUser()
        {
            InitializeComponent();
        }

        private void FrmUser_Load(object sender, EventArgs e)
        {
            controller = new Controller.EmployeController();
            controller.GetAllUsers(dataGridViewUsers);
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            if (dataGridViewUsers.SelectedRows.Count > 0)
            {
                DataGridViewRow row = dataGridViewUsers.SelectedRows[0];
                DiagUser dialog = new DiagUser(row.Cells["EmployeeID"].Value.ToString());
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    SetMessage("Record Updated");
                }
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (dataGridViewUsers.SelectedRows.Count > 0)
            {
                DataGridViewRow row = dataGridViewUsers.SelectedRows[0];

                string message = "Are you sure to remove " + row.Cells["EmployeeName"].Value.ToString() + " from user list?";

                if (MessageBox.Show(message, "Confirm User Removal", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    try {

                        controller.DeleteUser(row.Cells["EmployeeID"].Value.ToString());
                    
                        SetMessage("Record Deleted");
                        controller.GetAllUsers(dataGridViewUsers);

                    }
                    catch(Exception exc)
                    {
                        SetMessage(exc.Message);
                    }
                }
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (dataGridViewUsers.SelectedRows.Count > 0)
            {
                DataGridViewRow row = dataGridViewUsers.SelectedRows[0];
                DiagUser dialog = new DiagUser();
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    SetMessage("New User Added.");
                    controller.GetAllUsers(dataGridViewUsers);
                }
            }
        }

 
    }
}
